﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Pr0ject0.Models;
using Pr0ject0.ViewModel;

namespace Pr0ject0.Controllers
{
    [Authorize(Roles = "Kiosk")]
    public class JobApplicationController : Controller
    {
        private JobApplicationDB applications = JobApplicationDB.GetInstance();
        private JobModelDB jobs = JobModelDB.GetInstance();
        private ApplicantSessionManager sessions = ApplicantSessionManager.GetInstance();
        private QuestionAnswerDb qa = new QuestionAnswerDb();

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            List<int> jobIds = new List<int>();
            HttpCookie cookie = new HttpCookie("_tmpSession");

            foreach (string key in collection.Keys)
            {
                if (collection[key].Contains("true"))
                {
                    jobIds.Add(Convert.ToInt32(key));
                }
            }

            if (jobIds.Count == 0)
            {
                return RedirectToAction("Index");
            }

            cookie.Values["tmpid"] = sessions.GetUniqueId();
            cookie.Values["storeid"] = Membership.GetUser().UserName;
            cookie.Shareable = false;
            cookie.Expires = DateTime.UtcNow.AddSeconds(900.0d);//sets expiration for 7:30 from the current time
            this.Response.AppendCookie(cookie);

            sessions.AddJobIdArray(jobIds.ToArray<int>(), cookie.Values["tmpid"]);

            return Redirect("/JobApplication/Apply");
        }

        public ActionResult Index(string jobTitle, string department, string distance, string salary)
        {
            int _title;
            string _dept;
            int _dist;
            float _salary;
            Dictionary<int, string> titles = JobTitleDB.GetInstance().jobTitles;
            List<string> depts = new List<string>();


            if (jobTitle == "" || jobTitle == null)
            {
                _title = -1;
            }
            else
            {
                _title = Convert.ToInt32(jobTitle);
            }

            if (department == "" || department == null)
            {
                _dept = null;
            }
            else
            {
                _dept = department;
            }

            if (distance == "" || distance == null)
            {
                _dist = 20;
            }
            else
            {
                _dist = Convert.ToInt32(distance);
            }

            if (salary == "" || salary == null)
            {
                _salary = 0;
            }
            else
            {
                _salary = Convert.ToSingle(salary);
            }

            titles.Add(-1, "All");
            titles.Concat(JobTitleDB.GetInstance().jobTitles.ToList());
            titles.OrderBy(i => i.Key);
            ViewBag.titles = titles;
            depts.Add("");
            depts.AddRange(jobs.GetDepartments());
            ViewBag.dept = depts;
            ViewBag.dist = new int[] { 10, 20, 50, 100 };

            return View(jobs.GetJobsList(Membership.GetUser().UserName, _title, _dept, _dist, _salary));
        }

        //
        // GET: /JobApplication/Apply

        public ActionResult Apply()
        {
            return View();
        }

        //
        // POST: /JobApplication/Apply

        [HttpPost]
        public ActionResult Apply(ApplicationViewModel applicationmodel)
        {
            string sessionId = this.Request.Cookies["_tmpSession"].Values["tmpid"];
            List<JobApplicationModel> models;

            models = applicationmodel.CreateApplications(sessions.GetJobList(sessionId));
            sessions.AddApplications(models, sessionId);

            return RedirectToAction("Questionnaire");
        }

        public ActionResult Questionnaire()
        {
            string sessionId = this.Request.Cookies["_tmpSession"].Values["tmpid"];

            return View(qa.GetQuestionnaire(sessions.GetJobList(sessionId)));
        }

        [HttpPost]
        public ActionResult Questionnaire(FormCollection questions)
        {
            List<AnswerModel> answers = new List<AnswerModel>();
            string sessionId = this.Request.Cookies["_tmpSession"].Values["tmpid"];

            for (int i = 0; i < questions.Count / 2; i++)
            {
                AnswerModel temp = new AnswerModel();
                temp.answer = questions["answer_" + i];
                temp.question = questions["question_" + i];
                answers.Add(temp);
            }

            sessions.AddQuestionnaire(answers, sessionId);
            return RedirectToAction("Summary");
        }

        public ActionResult Summary()
        {
            return View();
        }

        protected override void HandleUnknownAction(string actionName)
        {
            this.RedirectToAction("Index");
        }
    }
}